<template>
  <!-- tags.vue 专用组件 -->
  <div class="container">
    <el-dialog
      :title="newForm.id ? '修改文章' : '新增文章'"
      :visible="showDialog"
      @close="closeClick"
      width="400px"
    >
      <el-form
        ref="disciplineForm"
        label-position="left"
        label-width="80px"
        :model="newForm"
      >
        <el-form-item
          label="所属学科"
          prop="subjectID"
          :rules="[
            { required: true, message: '请选择所属学科', trigger: 'blur' }
          ]"
        >
          <el-select
            placeholder="请选择"
            style="width: 100%"
            v-model="newForm.subjectID"
          >
            <el-option
              v-for="item in disciplineList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="tagName"
          :rules="[
            { required: true, message: '请输入目录名称', trigger: 'blur' }
          ]"
        >
          <el-input
            placeholder="请输入目录名称"
            v-model="newForm.tagName"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button @click="closeClick">取消</el-button>
        <el-button type="primary" @click="btnOK">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from "@/api/hmmm/subjects";
import { add, update, detail } from "@/api/hmmm/tags";

export default {
  props: {
    showDialog: {
      type: Boolean,
      require: true
    }
  },
  data() {
    return {
      newForm: {
        subjectID: "", // 学科id
        tagName: "" // 标签名称
      },
      disciplineList: [] // 学科列表
    };
  },
  methods: {
    closeClick() {
      this.$parent.showDialog = false;
      this.newForm = {
        subjectID: "", // 学科id
        tagName: "" // 标签名称
      };
    },
    async getDiscipline(id) {
      const { data } = await simple();
      this.disciplineList = data;
      if (id) {
        const { data } = await detail({ id });
        console.log(data);
        this.newForm = data;
      }
    },
    async btnOK() {
      this.$refs.disciplineForm.validate();
      if (this.newForm.id) {
        await update(this.newForm);
        this.$message.success("修改成功");
      } else {
        await add(this.newForm);
        this.$message.success("添加成功");
      }
      this.$parent.gettags();
      this.closeClick();
    }
  }
};
</script>

<style scoped lang="less"></style>
